package ml;

import hr.irb.fastRandomForest.FastRandomForest;

import java.awt.Graphics2D;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.ArrayUtils;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.MovingAverage;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.DefaultHighLowDataset;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.util.ArrayUtilities;

import util.Candle;
import util.Util;
import weka.classifiers.AbstractClassifier;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instances;

public class GoodTrades {

	private ArrayList<Candle> prices;
	private final int SIGN_PERIOD = 10;
	private final int TRADING_PERIOD = 10;
	
	public GoodTrades(String filename) {
	 createDataset(filename);
  }

 private void createDataset(String filename) {

	 try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename),"UTF-8"));
			br.readLine();
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
			String line;

			ArrayList<Candle> candles = new ArrayList<Candle>();
			while ((line = br.readLine()) != null) {
				StringTokenizer st = new StringTokenizer(line, ",");
				Date date = df.parse(st.nextToken());
				float priceOpen = Float.parseFloat(st.nextToken());
				float priceHigh = Float.parseFloat(st.nextToken());
				float priceLow = Float.parseFloat(st.nextToken());
				float priceClose = Float.parseFloat(st.nextToken());
				int volume = Integer.parseInt(st.nextToken());
				
				Candle candle = new Candle("", date, volume, priceHigh, priceLow, priceOpen, priceClose);
				candles.add(0, candle);
			}

			int numGoodTrades = 0;
			for (int i = 0; i < candles.size() - TRADING_PERIOD - 1; i++) {
	        	float buyingPrice = 0f;
	        	for (int j = i; j < i + SIGN_PERIOD; j++) {
					Candle candle = candles.get(j);
        			buyingPrice = candle.getPriceClosed();
	        	}
            	boolean isGoodTrade = true;
	        	for (int k = i + SIGN_PERIOD; k < i + SIGN_PERIOD + TRADING_PERIOD; k++) {
					Candle candle = candles.get(k);
					if (candle.getPriceLow() < buyingPrice) {
		            	isGoodTrade = false;
		            	break;
					}
	        	}
	        	if (isGoodTrade) {
	        		numGoodTrades++;
					System.out.println(numGoodTrades + "th good trade: " + df.format(candles.get(i+SIGN_PERIOD-1).getDate()));
	        	}
			}
	 }
	 catch (Exception e) {
		 e.printStackTrace();
	 }
  }

 public static void main(String args[]) {
	 GoodTrades frf = new GoodTrades(args[0]);
  }
}